//
//  ComposeViewController.m
//  微博
//
//  Created by zxy on 2018/12/29.
//  Copyright © 2018 zxy. All rights reserved.
//

#import "ComposeViewController.h"
#import "EmoticonsViewController.h"
@interface ComposeViewController ()<UITextViewDelegate,EmoticonsViewControllerDelegate>
@property(nonatomic,strong)UITextView *contentTV;
@property(nonatomic,strong)UILabel *placeHolderLabel;
@end

@implementation ComposeViewController

-(UITextView *)contentTV{
    if(!_contentTV){
        _contentTV = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _contentTV.delegate = self;
    }
    return _contentTV;
}

-(UILabel *)placeHolderLabel{
    if(!_placeHolderLabel){
        _placeHolderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 0, 0)];
        _placeHolderLabel.textColor = [UIColor lightGrayColor];
        _placeHolderLabel.text = @"分享新鲜事...";
        _placeHolderLabel.font = [UIFont systemFontOfSize:13];
        [_placeHolderLabel sizeToFit];
    }
    return _placeHolderLabel;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.contentTV becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.contentTV resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupNavigation];
    [self.view addSubview:self.contentTV];
    [self.contentTV addSubview:self.placeHolderLabel];
    
}

-(void)setupNavigation{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem setupNavigationItemTitle:@"关闭" target:self action:@selector(close)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem setupNavigationItemTitle:@"发送" target:self action:@selector(sendClick)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    self.navigationItem.title = @"极客江南";
    
    
    EmoticonsViewController *ctl = [[EmoticonsViewController alloc]init];
    ctl.delegate = self;
    //将表情控制器设置为子控制器，不然表情控制器页面所有点击事件都无效
    [self addChildViewController:ctl];
    self.contentTV.inputView = ctl.view;
}

-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)emoticonModel:(EmoticonsImageModel *)emoticonModel emoticonId:(NSString *)emId{
    if(emoticonModel.code != nil){//emoji表情
        NSLog(@"emoji表情");
        //replaceRange:emoji表情要显示的位置  self.contentTV.selectedTextRange:光标当前位置
        [self.contentTV replaceRange:self.contentTV.selectedTextRange withText:emoticonModel.emojiStr];
    }else{//图片表情
        NSLog(@"图片表情");
        NSString *emoticonPath = [[self EmoticonsPath:emId] stringByAppendingPathComponent: emoticonModel.png];
        NSLog(@"%@",emoticonPath);
    }
}


#pragma mark -发送一条微博消息
-(void)sendClick{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = access_token;
    params[@"status"] = self.contentTV.text;
    [PPNetworkHelper POST:@"https://api.weibo.com/2/statuses/update.json" parameters:params success:^(id responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSError *error){
        NSLog(@"error=%@",error);
    }];
}

#pragma mark -UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView{
    //textView.hasText:有内容返回yes，不然返回no
    self.placeHolderLabel.hidden = textView.hasText;
    self.navigationItem.rightBarButtonItem.enabled = textView.hasText;
}

-(NSString *)EmoticonsPath:(NSString *)emoticonsDirectoryName{
    return [[self EmoticonsBundlePath] stringByAppendingPathComponent:emoticonsDirectoryName];
}

#pragma mark - 表情bundel路径
-(NSString *)EmoticonsBundlePath{
    return [[NSBundle mainBundle] pathForResource:@"Emoticons.bundle" ofType:nil];
}
@end
